package com.example.cathouse;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.cathouse.databinding.EasterEggFragmentBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.IOException;
import java.sql.Time;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class EasterEggFragment extends Fragment {
    EasterEggViewModel eggViewModel;
    public static ProgressDialog dialog;
    private final String url = "https://service-azqaddv3-1304436300.cd.apigw.tencentcs.com/release/cqput-crx";
    private MutableLiveData<String> num;
    EasterEggFragmentBinding binding;

    public static EasterEggFragment newInstance() {
        return new EasterEggFragment();
    }

    @SuppressLint("SetTextI18n")
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(((AppCompatActivity) requireActivity()).getSupportActionBar()).hide();       //隐藏actionbar
        BottomNavigationView bottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setVisibility(View.GONE);
        eggViewModel = new ViewModelProvider(this).get(EasterEggViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.easter_egg_fragment, container, false);
        binding.setData(eggViewModel);
        binding.setLifecycleOwner(requireActivity());

        dialog = new ProgressDialog(requireContext());
        dialog.setMessage("数据加载ing");
        dialog.setTitle("请稍后");
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        num = new MutableLiveData<String>();

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        binding.textViewDay.setText(year+"-"+month+"-"+day);

        new Thread() {
            @Override
            public void run() {
                super.run();
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    num.postValue(Objects.requireNonNull(response.body()).string());
                } catch (IOException e) {
                    e.printStackTrace();
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }
            }
        }.start();
        num.observe(requireActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.textViewNum.setText(s);
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}